//
//  LHMainTabBarView.swift
//  LH-Swift-WeiBo
//
//  Created by liuhao on 16/9/22.
//  Copyright © 2016年 liuhao. All rights reserved.
//

import UIKit

class LHMainTabBarView: UITabBar {

    
    //定义闭包
    var composeClosure : (() -> ())?
    
 
    //重写方法创建对象
    
    override init(frame: CGRect){
    
        super.init(frame: frame)
        
        
        self.addSubview(composeBtn)
        
        //按钮添加点击事件
        composeBtn.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //按钮点击事件
    @objc private  func composeBtnClick() {
    

    //执行闭包可以传值
        composeClosure?()
        
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //定义变量 计算子视图的frame值
        
        let btnW = self.bounds.width / 5
        
        let btnH = self.bounds.height
        
        var index: CGFloat = 0
        
        
        //遍历子视图
        for subView in subviews{
            
            //判断子视图 是否是UITabBarButton
            if subView .isKind(of: NSClassFromString("UITabBarButton")!)
            {
                subView.frame = CGRect(x: btnW * index, y: 0, width: btnW, height: btnH)
                
                index += (index == 1) ? 2 : 1
            }
            
        }
        
        
        //设置 composeBtn的frame
        
        composeBtn.bounds.size = CGSize(width: btnW, height: btnH)
        composeBtn.center = CGPoint(x: self.center.x, y: self.bounds.height * 0.5)
        
    }
    
    
    //懒加载创建按钮
    
    private lazy var composeBtn : UIButton = {
    
        let btn = UIButton()
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        
        return btn
    }()
    
    
    
    

}
